/**
 * Created by songxiangyang on 2018/3/20.
 */

// <!-- 下载 -->
// var testDownload=function(){
//     $.ajax({
//         type: "GET",
//         url: "/center/testDownload",
//         cache: false,  //禁用缓存
//         data: '',  //传入组装的参数
//         dataType: "json",
//         success: function (result) {
//         }
//     });
// }

<!-- *************************CMPID上传************************ -->
var uploads=function(){
    var formData = new FormData(document.getElementById("form_table"));
    $.ajax({
        type: "POST",
        url: "/center/batchImport",
        cache: false,  //禁用缓存
        data: formData,  //传入组装的参数
        dataType: "json",
        contentType: false,
        processData: false,
        success: function (result) {
            var error = document.getElementById("error");
            error.setAttribute("class", "alert alert-danger");
            error.innerHTML = result.msg;
            initChannelList();
        }
    });
}


<!-- *************************公共模块************************ -->
var pageData = {
    tableUser0bj: null,
    tableChannel0bj: null,
    name: null
}

var dataSet = [
    ['产品类型','CD','产品词','2018-05-29 17:00:55'],
    ['产品类型','5G','凤舞短导航','2018-05-29 17:00:55'],
    ['产品类型','5J','凤舞短导航','2018-05-29 17:00:55'],
    ['产品类型','1L','凤舞导航','2018-05-29 17:00:55'],
    ['产品类型','R5','人群词','2018-05-29 17:00:55'],
    ['产品类型','RL','人群词','2018-05-29 17:00:55'],
    ['产品类型','2G','人群词人群词','2018-05-29 17:00:55'],
    ['产品类型','1L','人群词','2018-05-29 17:00:55'],
    ['产品类型','R5','人群词','2018-05-29 17:00:55'],
    ['产品类型','R5','人群词','2018-05-29 17:00:55'],
    ['产品类型','RL','人群词','2018-05-29 17:00:55'],
    ['产品类型','R5','人群词','2018-05-29 17:00:55'],
];

var dataUser = [
    ['2134565','qwqew','管理员','2018-05-29 17:00:55'],
    ['7564322','ytrter','用户','2018-05-29 17:00:55'],
    ['1243543','nbvcx','管理员，用户','2018-05-29 17:00:55'],
    ['2345623','wrerwe','用户','2018-05-29 17:00:55'],
    ['1234563','trerw','管理员，用户','2018-05-29 17:00:55'],
    ['9087656','ewrtasfg','用户','2018-05-29 17:00:55'],
    ['9876435','dfg','管理员','2018-05-29 17:00:55'],
    ['4356745','ouiyuty','管理员，用户','2018-05-29 17:00:55'],
    ['5465434','sdgfser','管理员，用户','2018-05-29 17:00:55'],
    ['7324313','werts','管理员','2018-05-29 17:00:55'],
    ['2345634','asdd','用户','2018-05-29 17:00:55'],
    ['3245643','wqere','管理员，用户','2018-05-29 17:00:55'],
];




layui.use('element', function(){
    var $ = layui.jquery
        ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
});

$(document).ready(function () {
    initUserList();
    initChannelList();
});
<!-- *************************渠道管理模块************************ -->
function deleteActionChannel() {

    var row = pageData.tableChannel0bj.rows( { selected: true } ).data();
    if (row['0'] != null){
        var r=confirm("确认删除吗")
        if (r==true) {
            // var el = document.getElementById("updateChannel");
            // el.setAttribute("data-toggle", "modal");
            var id = row['0']['id'];
            $.ajax({
                type: "GET",
                url: "/center/deleteChannel",
                cache: false,  //禁用缓存
                data: {id: id},  //传入组装的参数
                dataType: "json",
                success: function (result) {
                    initChannelList();
                }
            });
        }
    }else {
        swal("OMG!", "请选择一条记录", "error");
    }
}

function updateActionChannel() {
    if(checkSaveFormChannel('editChannelType','editCodeChannel','editNameChannel','errorUpdateDivChannel')){


        var ChannelId = $("#ChannelId").val();
        var editChannelType = $("#editChannelType").val();
        var editCodeChannel = $("#editCodeChannel").val();
        var editNameChannel = $("#editNameChannel").val();
        $("#modifyChannel").modal('hide');
                // $.ajax({
                //     type: "POST",
                //     url: "/center/modifyChannel",
                //     cache: false,  //禁用缓存
                //     data: {id:ChannelId,channelType:editChannelType,channelEn:editCodeChannel,channelCn:editNameChannel},  //传入组装的参数
                //     dataType: "json",
                //     success: function (result) {
                //         
                //         initChannelList();
                //     }
                // });
    }else {
        return false;
    }
}

function updateChannel() {
    var row = pageData.tableChannel0bj.rows( { selected: true } ).data();
    if (row['0'] != null){
        var el = document.getElementById("updateChannel");
        el.setAttribute("data-toggle", "modal");
        var id = row['0']['id'];
        $.ajax({
            type: "GET",
            url: "/center/channelInfo",
            cache: false,  //禁用缓存
            data: {id:id},  //传入组装的参数
            dataType: "json",
            success: function (result) {
                var channel = result;
                $("#ChannelId").val(channel.id);
                $("#editChannelType").val(channel.channelType);
                $("#editCodeChannel").val(channel.channelEn);
                $("#editNameChannel").val(channel.channelCn);
            }
        });
    }else {
        swal("OMG!", "请选择一条记录", "error");
    }
}

function queryChannel() {
    pageData.name = $("#channelName").val();
    initChannelList();
}

function initChannelList() {
    pageData.tableChannel0bj = $('#dataTablesChannel').DataTable({
        'data':dataSet,
        "destroy": true,
        select: true,
        "columns": [
            { title: "渠道类型" },
            { title: "渠道编码" },
            { title: "渠道名称" },
            { title: "更新时间." },
        ],
        // "columnDefs": [
        //                {
        //                    "targets": [0], //隐藏第六列，从第0列开始
        //                    "visible": false
        //                }
        //            ],
        // "dataSrc": 'staff',
        'paging': true,
        'lengthChange': false,
        'searching': false,
        'info': true,
        'autoWidth': false,
        // 'serverSide': true,
        // "bProcessing": true,
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "没有检索到数据",
            "sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",
            "sInfoEmtpy": "没有数据",
            "sProcessing": "正在加载数据...",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前页",
                "sNext": "后页",
                "sLast": "尾页"
            }
        }
        // ajax: {
        //     //封装请求参数
        //     type: "GET",
        //     url: "/center/loadsChannelList",
        //     cache: false, //禁用缓存
        //     data: {
        //         channelCn : pageData.name
        //     }
        // }, //传入组装的参数
            // dataType: "json",
        });
}

// function saveActionChannel() {
//     if(checkSaveFormChannel('channelType','codeChannel','nameChannel','errorDivChannel')){
//         $("#saveFormChannel").submit();
//     }else {
//         return false;
//     }
// }

function saveActionChannel() {
    if(checkSaveFormChannel('channelType','codeChannel','nameChannel','errorDivChannel')){


        var channelType = $("#channelType").val();
        var codeChannel = $("#codeChannel").val();
        var nameChannel = $("#nameChannel").val();
        $.ajax({
            type: "POST",
            url: "/center/saveChannel",
            cache: false,  //禁用缓存
            data: {channelType:channelType,channelEn:codeChannel,channelCn:nameChannel},  //传入组装的参数
            dataType: "json",
            success: function (result) {
                $("#addChannel").modal('hide');
                var error = document.getElementById("error");
                error.setAttribute("class", "alert alert-danger");
                error.innerHTML = result.msg;
                initChannelList();
            }
        });
        // $("#updateFormChannel").submit();
    }else {
        return false;
    }
}

function checkSaveFormChannel(channelType,codeChannel,nameChannel,errorDivChannel) {
    var nametip = checkChannelType(channelType,errorDivChannel);
    if (nametip) {
        var passtip = checkCodeChannel(codeChannel,errorDivChannel);
        if (passtip) {
            var passtip = checkNameChannel(nameChannel,errorDivChannel);
            if (passtip) {
                return true;
            }
            return false;
        }
        return false;
    }
    return false;
}


//验证渠道类型
function checkChannelType(tagName, errorDivName){
    var channelType = document.getElementById(tagName);
    var errname = document.getElementById(errorDivName);
    if(channelType.value.length == 0){
        errname.innerHTML="请选择渠道类型!"
        errname.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}

//验证渠道编码
function checkCodeChannel(tagName, errorDivName){
    var codeChannel = document.getElementById(tagName);
    var err = document.getElementById(errorDivName);
    if(codeChannel.value.length == 0){
        err.innerHTML="渠道编码不能为空!"
        err.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}

//验证渠道名称
function checkNameChannel(tagName,errorDivName){
    var channelName = document.getElementById(tagName);
    var errCon = document.getElementById(errorDivName);
    if(channelName.value.length == 0){
        errCon.innerHTML="渠道名称不能为空!"
        errCon.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}

<!-- *************************授权用户模块************************ -->


function checkSaveForm(tagUserName,tagPassword,tagSpassword,tagSelectedRole,errorDivName) {
    var nametip = checkUserName(tagUserName,errorDivName);
    if (nametip) {
        var passtip = checkPassword(tagPassword,errorDivName);
        if (passtip) {
            var passtip = ConfirmPassword(tagPassword,tagSpassword,errorDivName);
            if (passtip) {
                var isSelectedRole = checkRoleSelect(tagSelectedRole, errorDivName);
                if (isSelectedRole) {
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }
    return false;
}

//验证角色
function checkRoleSelect(tagName, errorDivName){
    var userpasswd = document.getElementById(tagName);
    var errPasswd = document.getElementById(errorDivName);
    if(userpasswd.value.length == 0){
        errPasswd.innerHTML="角色为必选项!"
        errPasswd.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}

//验证用户名
function checkUserName(tagName, errorDivName){
    var username = document.getElementById(tagName);
    var errname = document.getElementById(errorDivName);
    var pattern = /^\w{3,}$/;  //用户名格式正则表达式：用户名要至少三位
    if(username.value.length == 0){
        errname.innerHTML="用户名不能为空!"
        errname.setAttribute("class", "alert alert-danger");
        return false;
    }
    if(!pattern.test(username.value)){
        errname.innerHTML="用户名要至少三位并且不能是中文名称!"
        errname.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}
//验证密码
function checkPassword(tagName, errorDivName){
    var userpasswd = document.getElementById(tagName);
    var errPasswd = document.getElementById(errorDivName);
    var pattern = /^\w{4,8}$/; //密码要在4-8位
    if(userpasswd.value.length == 0){
        errPasswd.innerHTML="密码不能为空!"
        errPasswd.setAttribute("class", "alert alert-danger");
        return false;
    }
    if(!pattern.test(userpasswd.value)){
        errPasswd.innerHTML="密码要在4-8位!"
        errPasswd.setAttribute("class", "alert alert-danger");
        return false;
    }
    return true;
}

//确认密码
function ConfirmPassword(tagName,tagName2, errorDivName){
    var userpasswd = document.getElementById(tagName);
    var userConPassword = document.getElementById(tagName2);
    var errConPasswd = document.getElementById(errorDivName);
    if((userpasswd.value)!=(userConPassword.value) || userConPassword.value.length == 0){
        errConPasswd.setAttribute("class", "alert alert-danger");
        errConPasswd.innerHTML="上下密码不一致!"
        return false;
    }
    return true;
}


// function deleteAction() {

//     var row = pageData.tableUser0bj.rows( { selected: true } ).data();
//     if (row['0'] != null){
//         var r=confirm("确认删除吗")
//         if (r==true) {
//             var id = row['0']['id'];
//             $.ajax({
//                 type: "GET",
//                 url: "/center/deleteUser",
//                 cache: false,  //禁用缓存
//                 data: {id: id},  //传入组装的参数
//                 dataType: "json",
//                 success: function (result) {
//                     initUserList();
//                 }
//             });
//         }
//     }else {
//         swal("OMG!", "请选择一条记录", "error");
//     }
// }

function updateAction() {
    $("#modifyAccount").modal('hide');
    // if(checkSaveForm('editUserName','editPasswd','editSPasswd','editSelectRole','errorUpdateDiv')){
    //     var userId = $("#id").val();
    //     var editUserName = $("#editUserName").val();
    //     var editPasswd = $("#editPasswd").val();
    //     var roleValues = $("#editSelectRole").val()+'';
    //     $.ajax({
    //         type: "POST",
    //         url: "/center/modifyUser",
    //         cache: false,  //禁用缓存
    //         data: {id:userId,username:editUserName,password:editPasswd,roleValues:roleValues},  //传入组装的参数
    //         dataType: "json",
    //         success: function (result) {
    //             $("#modifyAccount").modal('hide');
    //             initUserList();
    //         }
    //     });
    // }else {
    //     return false;
    // }
}



// function update() {
//     var row = pageData.tableUser0bj.rows( { selected: true } ).data();
//     if (row['0'] != null){
//         var el = document.getElementById("updateA");
//         el.setAttribute("data-toggle", "modal");
//         var id = row['0']['id'];
//         $.ajax({
//             type: "GET",
//             url: "/center/userInfo",
//             cache: false,  //禁用缓存
//             data: {id:id},  //传入组装的参数
//             dataType: "json",
//             success: function (result) {
//                 var user = result;
//                 $("#id").val(user.id);
//                 $("#editUserName").val(user.username);
//                 // $("#editRoleId").val(user.role);
//                 var str=user.roleValues;
//                 var arr=str.split(',');
//                 $("#editSelectRole").selectpicker('val', arr);
//             }
//         });
//     }else {
//         swal("OMG!", "请选择一条记录", "error");
//     }
// }

function query() {
    pageData.name = $("#name").val();
    initUserList();
}



function saveAction() {
    $("#addAccount").modal('hide');
    // if(checkSaveForm('username','password','spassword','selectRole','errorDiv')){


    //     var username = $("#username").val();
    //     var roleValues = $("#selectRole").val()+'';
    //     var password = $("#password").val();
    //     $.ajax({
    //         type: "POST",
    //         url: "/center/saveUser",
    //         cache: false,  //禁用缓存
    //         data: {username:username,roleValues:roleValues,password:password},  //传入组装的参数
    //         dataType: "json",
    //         success: function (result) {
    //             $("#addAccount").modal('hide');
    //             var error = document.getElementById("error");
    //             error.setAttribute("class", "alert alert-danger");
    //             error.innerHTML = result.msg;
    //             initUserList();
    //         }
    //     });
    // }else {
    //     return false;
    // }
}


function initUserList() {
    pageData.tableUser0bj = $('#dataTables').DataTable({
         "data": dataUser,
        // "destroy": true,
        // select: true,
        "columns": [
            { "title": "ID" },
            { "title": "用户名" },
            { "title": "角色" },
            { "title": "加入时间", "class": "center" },
        ],
        "dataSrc": 'staff',
        'paging': true,
        'lengthChange': false,
        'searching': false,
        'ordering': true,
        'info': true,
        'autoWidth': false,
        // 'serverSide': true,
        // "bProcessing": true,
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "没有检索到数据",
            "sInfo": "当前数据为从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录",
            "sInfoEmtpy": "没有数据",
            "sProcessing": "正在加载数据...",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前页",
                "sNext": "后页",
                "sLast": "尾页"
            }
        },
        // ajax: function (data, callback, settings) {
        //     //封装请求参数
        //     var param = {};
        //     param.limit = data.length; //页面显示记录条数，在页面显示每页显示多少项的时候
        //     param.currentPageNum = (data.start / data.length) + 1; //当前页码
        //     param.username = pageData.name;
        //     $.ajax({
        //         type: "GET",
        //         url: "/center/loadsUserList",
        //         cache: false, //禁用缓存
        //         data: param, //传入组装的参数
        //         dataType: "json",
        //         success: function (result) {
        //             callback(result);
        //         }
        //     });
        // }
    });
}



